﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Xml.Serialization;

namespace Qing.QLoggers
{
    public abstract class QLogger
    {
        private string source = "logs/{0}.log";
        public string Source { set { source = value; } get { return string.Format(source, DateTime.Now.ToString("yyyy-MM-dd")); } }

        public static QLogger Create(QLogType logType)
        {
            QLogger res = null;
            switch (logType)
            {
                case QLogType.Text: res = new TextLogger(); break;
                default: break;
            }
            return res;
        }
        protected abstract void Write(QLogInfo logInfo);

        private object _lock = new object();
        public void Log(QLogInfo logInfo)
        {
            if (logInfo == null) return;
            lock (_lock)
            {
                try { Write(logInfo); }
                catch { }
            }
        }
    }

#if DEBUG
    static class Program
    {
        static void Main(string[] args)
        {
            var txtLog = QLogger.Create(QLogType.Text);
            Action<int> action = p => { new Thread(new ThreadStart(() => { for (int i = p*1000; i < (p+1) * 1000; i++) txtLog.Log(new QLogInfo() { Code = i }); })) { IsBackground = true }.Start(); };
            for (int i = 1; i <= 5; i++) action(i);

            Console.WriteLine("\n\nEnd.");
            Console.ReadKey();
        }
    }
}
#endif
